#!usr/bin/env python
# -*- coding:utf8 -*-

from matplotlib.colors import LinearSegmentedColormap
from getSpectrum import getSpectrum  
import numpy, matplotlib.pyplot as plt
# 窗长20ms， 窗移时窗长的0.5倍
speech_spectrum = getSpectrum('untitled.wav', 20, 0.5)  
#noise_spectrum = getSpectrum('noise.wav', 20, 0.5)  
#noised_speech_spectrum = speech_spectrum[:,:300] + noise_spectrum[:, :300]
#######################################################################
# plot white-black spectrum like praat
def CustomCmap(from_rgb,to_rgb):

    # from color r,g,b
    r1,g1,b1 = from_rgb

    # to color r,g,b
    r2,g2,b2 = to_rgb

    cdict = {'red': ((0, r1, r1),
                   (1, r2, r2)),
           'green': ((0, g1, g1),
                    (1, g2, g2)),
           'blue': ((0, b1, b1),
                   (1, b2, b2))}

    cmap = LinearSegmentedColormap('custom_cmap', cdict)
    return cmap


fig, ax = plt.subplots(2,2, figsize=(6,6), subplot_kw={'xticks': [],'yticks': []})
fig.subplots_adjust(hspace=.1,wspace=.1)

cmap1 = CustomCmap([0.00, 0.00, 0.00], [1.00, 1.00, 1.00]) # from black to +/- 5,192,255

plt.imshow(speech_spectrum, interpolation='none', cmap=cmap1)

fig.savefig("temp.png")

#######################################################################
# original code
#plt.plot()
#plt.subplot(511)  
#plt.imshow(speech_spectrum)
#plt.savefig("test.jpg")
#plt.subplot(312)  
#plt.imshow(noise_spectrum)
#plt.subplot(313)  
#plt.imshow(noised_speech_spectrum)
